<?php

class ome_store_manage_store extends ome_store_manage_abstract implements ome_store_manage_interface{

    /**
     * 添加发货单节点的库存处理方法
     *
     * @param array $params 传入参数
     * @param string $err_msg 错误信息
     * @return boolean true/false
     */
    public function addDly($params, &$err_msg){
        //门店管控库存
        if($this->_is_ctrl_store){
            $storeBranchObj        = app::get('o2o')->model("product_store");
            $branchPrdLib        = kernel::single('o2o_branch_product');
            $basicMStockFreezeLib = kernel::single('material_basic_material_stock_freeze');

            foreach($params['delivery_items'] as $key=>$dly_item)
            {
                //识别门店货品是否管控库存，不管控直接处理下一个货品
                $is_bm_ctrl_store = $branchPrdLib->isCtrlBmStore($params['branch_id'],$dly_item['product_id']);
                if($is_bm_ctrl_store){
                    $sql = "SELECT sum( IF( store < store_freeze, 0, store - store_freeze ) ) AS 'has' FROM sdb_o2o_product_store 
                            WHERE bm_id=".$dly_item['product_id']." AND branch_id =".$params['branch_id'];
                    $store_p = $storeBranchObj->db->selectrow($sql);

                    if (!is_numeric($dly_item['number']))
                    {
                        $err_msg    .= $dly_item['product_name'] .":请输入正确数量";
                        return false;
                    }

                    if (empty($store_p['has']) || $store_p['has'] == 0 ||  $store_p['has'] < $dly_item['number'])
                    {
                        $err_msg    .= $dly_item['product_name'] .":商品库存不足";
                        return false;
                    }

                    //门店货品预占冻结
                    $basicMStockFreezeLib->freeze($dly_item['product_id'], material_basic_material_stock_freeze::__BRANCH, $params['delivery_id'], $params['shop_id'], $params['branch_id'], material_basic_material_stock_freeze::__STORE_CONFIRM, $dly_item['number']);

                    //订单货品预占释放
                    $basicMStockFreezeLib->unfreeze($dly_item['product_id'], material_basic_material_stock_freeze::__ORDER, $params['order_id'], '', material_basic_material_stock_freeze::__SHARE_STORE, $dly_item['number']);
                }else{
                    continue;
                }
            }
        }else{
            //不管控库存情况下，发货单生成不做任何库存预占释放增加的处理
        }

        return true;
    }

    /**
     * 取消发货单节点的库存处理方法
     *
     * @param array $params 传入参数
     * @param string $err_msg 错误信息
     * @return boolean true/false
     */
    public function cancelDly($params, &$err_msg){
        //门店管控库存
        if($this->_is_ctrl_store){
            $branchPrdLib        = kernel::single('o2o_branch_product');
            $basicMStockFreezeLib = kernel::single('material_basic_material_stock_freeze');

            foreach($params['delivery_items'] as $key=>$dly_item){
                //识别门店供货货品是否管控库存，管控的：释放门店货品冻结，增加订单货品冻结，不管控的：不做任何处理
                //识别门店货品是否管控库存，不管控直接处理下一个货品
                $is_bm_ctrl_store = $branchPrdLib->isCtrlBmStore($params['branch_id'],$dly_item['product_id']);
                if($is_bm_ctrl_store){
                    $rs = $basicMStockFreezeLib->unfreeze($dly_item['product_id'], material_basic_material_stock_freeze::__BRANCH, $params['delivery_id'], $params['branch_id'], material_basic_material_stock_freeze::__STORE_CONFIRM, $dly_item['number']);
                    if ($rs == false) {
                        $err_msg    = '门店货品冻结释放失败!';
                        return false;
                    }

                    
                    $rs = $basicMStockFreezeLib->freeze($dly_item['product_id'], material_basic_material_stock_freeze::__ORDER, $params['order_id'], $params['shop_id'], 0, material_basic_material_stock_freeze::__SHARE_STORE, $dly_item['number']);
                    if ($rs == false) {
                        $err_msg    = '订单货品预占添加失败!';
                        return false;
                    }
                }else{
                    continue;
                }
            }

            //发货单取消，门店仓预占流水删除
            $basicMStockFreezeLib->delDeliveryFreeze($params['delivery_id']);
        }else{
            //不管控库存情况下，发货单取消不做任何库存预占释放增加的处理
        }

        return true;
    }

    /**
     * 发货单发货节点的库存处理方法
     *
     * @param array $params 传入参数
     * @param string $err_msg 错误信息
     * @return boolean true/false
     */
    public function consignDly($params, &$err_msg){
        $branchPrdLib        = kernel::single('o2o_branch_product');
        $basicMStockLib = kernel::single('material_basic_material_stock');
        $basicMStockFreezeLib = kernel::single('material_basic_material_stock_freeze');

        //门店管控库存
        if($this->_is_ctrl_store){
            foreach($params['delivery_items'] as $key=>$dly_item){
                //识别门店供货货品是否管控库存，管控的：释放门店货品冻结，扣减门店货品实际库存，不管控的：释放订单货品冻结
                $is_bm_ctrl_store = $branchPrdLib->isCtrlBmStore($params['branch_id'],$dly_item['product_id']);
                if($is_bm_ctrl_store){
                    $rs = $basicMStockFreezeLib->unfreeze($dly_item['product_id'], material_basic_material_stock_freeze::__BRANCH, $params['delivery_id'], $params['branch_id'], material_basic_material_stock_freeze::__STORE_CONFIRM, $dly_item['number']);
                    if ($rs == false) {
                        $err_msg    = '门店货品冻结释放失败!';
                        return false;
                    }

                    $rs    = $basicMStockLib->unfreeze($dly_item['product_id'], $dly_item['number']);
                    if ($rs == false)
                    {
                        $err_msg    = '货品冻结释放更新失败!';
                        return false;
                    }

                    //发货单发货，门店仓预占流水删除
                    $basicMStockFreezeLib->delDeliveryFreeze($params['delivery_id']);

                    //门店货品实际库存扣减
                    $branchPrdLib->changeStoreConfirmStore($params['branch_id'],$dly_item['product_id'],$dly_item['number'],'-');
                }else{
                    $rs = $basicMStockFreezeLib->unfreeze($dly_item['product_id'], material_basic_material_stock_freeze::__ORDER, $params['order_id'], '', material_basic_material_stock_freeze::__SHARE_STORE, $dly_item['number']);
                    if ($rs == false) {
                        $err_msg    = '订单货品冻结释放更新失败!';
                        return false;
                    }

                    $rs    = $basicMStockLib->unfreeze($dly_item['product_id'], $dly_item['number']);
                    if ($rs == false)
                    {
                        $err_msg    = '货品冻结释放更新失败!';
                        return false;
                    }
                }
            }
        }else{
            //门店仓不管控库存，释放订单货品冻结，释放货品冻结
            foreach($params['delivery_items'] as $key=>$dly_item){
                $rs = $basicMStockFreezeLib->unfreeze($dly_item['product_id'], material_basic_material_stock_freeze::__ORDER, $params['order_id'], '', material_basic_material_stock_freeze::__SHARE_STORE, $dly_item['number']);
                if ($rs == false) {
                    $err_msg    = '订单货品冻结释放更新失败!';
                    return false;
                }

                $rs    = $basicMStockLib->unfreeze($dly_item['product_id'], $dly_item['number']);
                if ($rs == false)
                {
                    $err_msg    = '货品冻结释放更新失败!';
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * 订单暂停发货单取消节点的库存处理方法
     *
     * @param array $params 传入参数
     * @param string $err_msg 错误信息
     * @return boolean true/false
     */
    public function pauseOrd($params, &$err_msg){
        //门店管控库存
        if($this->_is_ctrl_store){
            $branchPrdLib        = kernel::single('o2o_branch_product');
            $basicMStockFreezeLib = kernel::single('material_basic_material_stock_freeze');

            foreach($params['delivery_items'] as $key=>$dly_item){
                //识别门店供货货品是否管控库存，管控的：释放门店货品冻结，增加订单货品冻结，不管控的：不做任何处理
                //识别门店货品是否管控库存，不管控直接处理下一个货品
                $is_bm_ctrl_store = $branchPrdLib->isCtrlBmStore($params['branch_id'],$dly_item['product_id']);
                if($is_bm_ctrl_store){
                    $rs = $basicMStockFreezeLib->unfreeze($dly_item['product_id'], material_basic_material_stock_freeze::__BRANCH, $params['delivery_id'], $params['branch_id'], material_basic_material_stock_freeze::__STORE_CONFIRM, $dly_item['number']);
                    if ($rs == false) {
                        $err_msg    = '门店货品冻结释放失败!';
                        return false;
                    }

                    
                    $rs = $basicMStockFreezeLib->freeze($dly_item['product_id'], material_basic_material_stock_freeze::__ORDER, $params['order_id'], $params['shop_id'], 0, material_basic_material_stock_freeze::__SHARE_STORE, $dly_item['number']);
                    if ($rs == false) {
                        $err_msg    = '订单货品预占添加失败!';
                        return false;
                    }
                }else{
                    continue;
                }
            }

            //订单暂停发货单取消，门店仓预占流水删除
            $basicMStockFreezeLib->delDeliveryFreeze($params['delivery_id']);
        }else{
            //不管控库存情况下，发货单取消不做任何库存预占释放增加的处理
        }

        return true;
    }

    /**
     * 订单恢复节点的库存处理方法
     *
     * @param array $params 传入参数
     * @param string $err_msg 错误信息
     * @return boolean true/false
     */
    public function renewOrd($params, &$err_msg){
        //和电商仓一样，nothing need to do
    }
}